잠시만 기다려 주세요

     '민주당이 분열되어야 정치개혁이 시작된다.'
전체검색 :  
이번주 로또 및 연금번호 발생!!   |  HOME   |  여기는?   |  바다물때표   |  알림 (16)  |  여러가지 팁 (1054)  |  추천 및 재미 (150)  |  자료실 (22)  |  
시사, 이슈, 칼럼, 평론, 비평 (582)  |  끄적거림 (127)  |  문예 창작 (702)  |  바람 따라 (69)  |  시나리오 (760)  |  드라마 대본 (248)  |  
살인!


    postgresql

postgresql - postgresql 엑셀 CSV 화일로 테이블 export/import 하기
이 름 : 바다아이   |   조회수 : 8515         짧은 주소 : https://www.bada-ie.com/su/?41591823371
테이블 데이터를 텍스트화일로 export 해보겠습니다.
 
[postgres@olmaster:~/oradba/csv]$ alias scott
alias scott='psql -d scottdb -U scott -W'
 
[postgres@olmaster:~/oradba/csv]$ scott
Password for user scott: 
Null display is "NULL".
Timing is on.
Pager is always used.
psql (9.2.1)
Type "help" for help.
 
scott@[local]:5432 scottdb#SQL> \copy emp to '~/oradba/csv/emp1.csv';
Time: 1.570 ms
scott@[local]:5432 scottdb#SQL> \! cat ~/oradba/csv/emp1.csv
7369    SMITH   CLERK   7902    1980-12-17      800     \N      20
7499    ALLEN   SALESMAN        7698    1981-02-20      1600    300     30
7521    WARD    SALESMAN        7698    1981-02-22      1250    500     30
7566    JONES   MANAGER 7839    1981-04-02      2975    \N      20
7654    MARTIN  SALESMAN        7698    1981-09-28      1250    1400    30
7698    BLAKE   MANAGER 7839    1981-05-01      2850    \N      30
7782    CLARK   MANAGER 7839    1981-06-09      2450    \N      10
7788    SCOTT   ANALYST 7566    1982-12-09      3000    \N      20
7839    KING    PRESIDENT       \N      1981-11-17      5000    \N      10
7844    TURNER  SALESMAN        7698    1981-09-08      1500    0       30
7876    ADAMS   CLERK   7788    1983-01-12      1100    \N      20
7900    JAMES   CLERK   7698    1981-12-03      950     \N      30
7902    FORD    ANALYST 7566    1981-12-03      3000    \N      20
7934    MILLER  CLERK   7782    1982-01-23      1300    \N      10
scott@[local]:5432 scottdb#SQL> \copy emp(empno,ename) to '~/oradba/csv/emp2.csv';
Time: 0.480 ms
scott@[local]:5432 scottdb#SQL> \! cat ~/oradba/csv/emp2.csv
7369    SMITH
7499    ALLEN
7521    WARD
7566    JONES
7654    MARTIN
7698    BLAKE
7782    CLARK
7788    SCOTT
7839    KING
7844    TURNER
7876    ADAMS
7900    JAMES
7902    FORD
7934    MILLER
scott@[local]:5432 scottdb#SQL> \copy emp(empno,ename) to '~/oradba/csv/emp3.csv' with delimiter ',' csv header
Time: 0.357 ms
scott@[local]:5432 scottdb#SQL> \! cat ~/oradba/csv/emp3.csv
empno,ename
7369,SMITH
7499,ALLEN
7521,WARD
7566,JONES
7654,MARTIN
7698,BLAKE
7782,CLARK
7788,SCOTT
7839,KING
7844,TURNER
7876,ADAMS
7900,JAMES
7902,FORD
7934,MILLER
scott@[local]:5432 scottdb#SQL> 
 
이제 새 테이블을 생성후에, emp3.csv 화일을  import 해보겠습니다.
 
[postgres@olmaster:~/oradba/csv]$ cat emp3.csv 
empno,ename
7369,SMITH
7499,ALLEN
7521,WARD
7566,JONES
7654,MARTIN
7698,BLAKE
7782,CLARK
7788,SCOTT
7839,KING
7844,TURNER
7876,ADAMS
7900,JAMES
7902,FORD
7934,MILLER
[postgres@olmaster:~/oradba/csv]$ scott
Password for user scott: 
Null display is "NULL".
Timing is on.
Pager is always used.
psql (9.2.1)
Type "help" for help.
 
scott@[local]:5432 scottdb#SQL> create table customer(no integer, name varchar(32));
CREATE TABLE
Time: 101.081 ms
scott@[local]:5432 scottdb#SQL> \copy customer(no,name) from '~/oradba/csv/emp3.csv' with delimiter ',' csv header
Time: 1.632 ms
scott@[local]:5432 scottdb#SQL> select * from customer;
  no  |  name  
------+--------
 7369 | SMITH
 7499 | ALLEN
 7521 | WARD
 7566 | JONES
 7654 | MARTIN
 7698 | BLAKE
 7782 | CLARK
 7788 | SCOTT
 7839 | KING
 7844 | TURNER
 7876 | ADAMS
 7900 | JAMES
 7902 | FORD
 7934 | MILLER
(14 rows)
 
Time: 0.481 ms
scott@[local]:5432 scottdb#SQL> 
 
오라클에 비하면, PostgreSQL 의 이런 기능은 너무 좋네요.
 
 
-----------------2013.07.12 추가 ------------------
 
필드값 내에 "," 가 들어가 있다면 다음과 같이 하면 됩니다.
[enterprisscottdb@dpcpdb03 oradba]$ cat a.csv
1,2,"super,man","spider","man"
 
[enterprisscottdb@dpcpdb03 oradba]$ psql
psql (9.2.4.8)
Type "help" for help.
 
scottdb=# create table ccc(a varchar(10), b varchar(10), c varchar(10), d varchar(10), e varchar(10));
CREATE TABLE
scottdb=# \copy ccc(a,b,c,d,e) from '~/oradba/a.csv' with csv  quote '"'
scottdb=# select * from ccc;
 a | b |     c     |   d    |  e  
---+---+-----------+--------+-----
 1 | 2 | super,man | spider | man
(1 row)
 
scottdb=# 
 
cf. 컬럼변환이 필요하면
 \copy ( SELECT CUST_NO ,TO_CHAR(REG_DTTM,'YYYY-MM-DD HH24:MI:SS') FROM ZZZ) to '/archive/dw.dat' with delimiter '|'
 
cf. export
copy (select * from from sql_trace
where snap_dt >= '$DATE2'
and snap_dt < '$DATE3'
order by snap_dt)
to '/Postgres/9.2/oradba/sql_move.csv' CSV QUOTE '"'



출처 : http://www.postgresdba.com/bbs/board.php?bo_table=B13&wr_id=4
 
| |





      1 page / 2 page
번 호 카테고리 제 목 이름 조회수
45 postgresql postgresql ... postgresql 14 .. postgresql.conf port 5432 .. 바다아이 676
44 postgresql , count(*) .... 바다아이 6854
43 postgresql How to do an update + join in PostgreSQL?, 바다아이 6085
42 postgresql sequence(퀀) 바다아이 7718
41 postgresql , , , index create, , 바다아이 8100
40 postgresql postgresql log_timezone .... 바다아이 6504
39 postgresql postgresql SEQUENCE reset .... 바다아이 8340
38 postgresql [PostgreSql] WITH , , Operator 바다아이 7730
37 postgresql postgresql for, foreach , 바다아이 9284
36 postgresql postgresql , , into ... 바다아이 9282
35 postgresql postgresql PL/pgSQL - SQL Procedural Language, , 바다아이 10697
34 postgresql postgresql ... .. , , 바다아이 11711
현재글 postgresql postgresql CSV export/import 바다아이 8516
32 postgresql postgresql tablespace , .... 바다아이 12838
31 postgresql postgresql 10 partitioning, ... , ... 바다아이 9254
30 postgresql Using PostgreSQL Arrays, ... ... 바다아이 9222
29 postgresql PostgreSQL (TRIGGER) (function) 바다아이 8984
28 postgresql Optimize and Improve PostgreSQL Performance with VACUUM, ANALYZE, and REINDEX 바다아이 9633
27 postgresql postgresql tuple . vacuumdb .. , . 바다아이 9030
26 postgresql postgresql , .. 바다아이 9225
25 postgresql postgresql , size, 바다아이 11223
24 postgresql postgresql , , .... 바다아이 8271
23 postgresql PostgreSQL Replication, , , master, slave 바다아이 10771
22 postgresql postgresql case 바다아이 8138
21 postgresql postgresql with 바다아이 8575
20 postgresql postgresql , , string 바다아이 11482
19 postgresql Postgresql partitioning table , , , 바다아이 9074
18 postgresql PostgreSQL 바다아이 10563
17 postgresql postgresql vacuumdb, psql, pg_dump password crontab , pgpass 바다아이 10248
16 postgresql postgresql sequence 퀀 auto_increment . 바다아이 9666
| |









Copyright ⓒ 2001.12. bada-ie.com. All rights reserved.
이 사이트는 리눅스에서 firefox 기준으로 작성되었습니다. 기타 브라우저에서는 다르게 보일 수 있습니다.
[ Ubuntu + GoLang + PostgreSQL + Mariadb ]
서버위치 : 오라클 클라우드 춘천  실행시간 : 0.05915
to webmaster... gogo sea. gogo sea.